package com.ee.chat.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ee.chat.admin.service.IGiftService;
import com.ee.chat.admin.service.IOperationService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.GiftConfigMapper;
import com.ee.chat.common.entity.GiftConfig;
import com.ndhutu.ee.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author AppiJ
 * @date 2019年10月30日 19:45
 */
@Service
public class GiftServiceImpl implements IGiftService {

    @Resource
    GiftConfigMapper giftConfigMapper;
    @Resource
    IOperationService operationService;

    /**
     * 查询礼物列表
     * @author AppiJ
     * @date 2019年10月30日 21:02:40
     * @param keyword 搜索关键词
     * @param status 状态
     * @return
     */
    @Override
    public Result list(HttpServletRequest request, String keyword, Integer status, Integer page, Integer limit) throws Exception {

        Map<String, Object> data = new HashMap<>();

        if(StringUtil.isEmpty(keyword) && status == null){
            HttpSession session = request.getSession(true);
            List<String> userPermissions = (List<String>)session.getAttribute("userPermissions");
            if(!userPermissions.contains("/gift/viewList")){
                data.put("gifts", new ArrayList<>());
                data.put("total", 0);

                return Result.returnSuccess(data);
            }
        }

        Page<Map<String, Object>> p = new Page<>(page, limit);
        IPage<Map<String, Object>> iPage = giftConfigMapper.list(p, keyword, status);
        List<Map<String, Object>> gifts = iPage.getRecords();

        data.put("gifts", gifts);
        data.put("total", iPage.getTotal());

        operationService.addOperationLog(request, "获取礼物数据列表");

        return Result.returnSuccess(data);
    }

    /**
     * 添加/编辑礼物
     * @author AppiJ
     * @date 2019年11月01日 11:53:24
     * @param giftConfig
     * @return
     */
    @Override
    public Result add(HttpServletRequest request, GiftConfig giftConfig) throws Exception {

        if(giftConfig.getId() == null){
            //添加礼物
            giftConfigMapper.insert(giftConfig);

            operationService.addOperationLog(request, "新增礼物，id："+giftConfig.getId());
        }else {
            //修改礼物
            giftConfigMapper.updateById(giftConfig);

            operationService.addOperationLog(request, "编辑礼物，id："+giftConfig.getId());
        }

        return Result.returnSuccess(null);
    }

    /**
     * 去编辑礼物
     * @author AppiJ
     * @date 2019年11月07日 16:45:03
     * @param id  礼物id
     * @return
     */
    @Override
    public Result toEdit(HttpServletRequest request, Integer id) throws Exception {

        Map<String, Object> data = new HashMap<>();

        GiftConfig gift = giftConfigMapper.selectById(id);
        gift = gift == null ? new GiftConfig() : gift;

        data.put("gift", gift);

        return Result.returnSuccess(data);
    }

    /**
     * 上架/下架礼物
     * @author AppiJ
     * @date 2019年11月08日 18:38:31
     * @param ids     id列表
     * @param status  状态
     * @return
     */
    @Override
    public Result updateStatus(HttpServletRequest request, List<Integer> ids, Integer status) throws Exception {

        for(Integer id : ids){
            GiftConfig giftConfig = giftConfigMapper.selectById(id);
            giftConfig.setStatus(status);
            giftConfigMapper.updateById(giftConfig);
        }

        operationService.addOperationLog(request, "变更礼物状态，id："+ids);

        return Result.returnSuccess(null);
    }
}
